package com.gzj.springboot.controller;

import com.gzj.springboot.domain.OftenCities;
import com.gzj.springboot.mapper.OftenCitiesMapper;
import com.gzj.springboot.service.OftenCitiesservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Controller
public class OftenCitiesController {
    /**
     * 注入service
     */
    @Autowired
    private OftenCitiesservice oftenCitiesservice;

    /**
     * 跳转到查询页面
     */
    @RequestMapping(value = {"/tocityquery"})
    public String cityQuery(){return "cityquery";}

    /**
     * 跳转到添加页面
     */
    @RequestMapping(value = {"/tocityadd"})
    public String cityAdd(){return "cityadd";}

    /**
     * 根据用户id查询
     * @param user_name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cityquery")
    public List<OftenCities> querycity(@RequestParam("user_name") String user_name){
        List<OftenCities> oftenCities = oftenCitiesservice.queryCity(user_name);
        return oftenCities;
    }

    /**
     * 根据用户账号添加常用城市
     * @param city
     * @param user_name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/cityadd"})
    public String addcity(@RequestParam("city") String city,@RequestParam("user_name") String user_name){
        if (StringUtils.isEmpty(user_name)){
            return "用户名不能为空";
        }
        if (StringUtils.isEmpty(city)){
            return "城市不能为空";
        }
        int res = oftenCitiesservice.addCity(city,user_name);
        if (res==0){
            return "该城市已添加";
        }else {
            return "添加成功";
        }
    }

    /**
     * 删除常用城市
     * @param city
     * @param user_name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/deleteCity"})
    public String deleteCity(@RequestParam("city") String city,@RequestParam("user_name") String user_name){
        if (StringUtils.isEmpty(user_name)){
            return "用户名不能为空";
        }
        if (StringUtils.isEmpty(city)){
            return "城市不能为空";
        }
        int res = oftenCitiesservice.deleteCity(city,user_name);
        if (res==1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
}
